package wcommons.httputils;

import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;

import wcommons.lang.SystemUtils;
import wcommons.lang.properties.PropertiesUtils;

/**
 * @author Wayne.Wang<5waynewang@gmail.com>
 * @since 11:03:32 AM Nov 25, 2013
 */
public class HttpClientFactory {

	protected final Log log = LogFactory.getLog(getClass());

	private final PoolingClientConnectionManager conman = new PoolingClientConnectionManager();

	private final HttpParams params;

	public HttpClientFactory() {
		this.params = HttpUtils.createHttpParams();

		this.conman.setMaxTotal(SystemUtils.getSystemPropertyForInt(HttpStatics.MAX_TOTAL_CONNECTIONS,
				HttpStatics.DEFAULT_MAX_TOTAL_CONNECTIONS));
		this.conman.setDefaultMaxPerRoute(SystemUtils.getSystemPropertyForInt(HttpStatics.MAX_CONNECTIONS_PER_ROUTE,
				HttpStatics.DEFAULT_MAX_CONNECTIONS_PER_ROUTE));
	}

	public HttpClientFactory(Properties properties) {
		this.params = HttpUtils.createHttpParams(properties);

		this.conman.setMaxTotal(PropertiesUtils.getPropertyForInteger(properties, HttpStatics.MAX_TOTAL_CONNECTIONS,
				HttpStatics.DEFAULT_MAX_TOTAL_CONNECTIONS));
		this.conman.setDefaultMaxPerRoute(PropertiesUtils.getPropertyForInteger(properties,
				HttpStatics.MAX_CONNECTIONS_PER_ROUTE, HttpStatics.DEFAULT_MAX_CONNECTIONS_PER_ROUTE));
	}

	public HttpClient create() {
		return new DefaultHttpClient(this.conman, this.params);
	}

	public String httpContentCharset() {
		return (String) this.params.getParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET);
	}
}
